<template>
	<view class="pt14 h100 column">
		<view class="flex-1">
			<view class="money">
				<view class="title">提现金额</view>
				<view class="num">
					<text class="flag">￥</text>
					<input type="digit" class="input-ref" placeholder="提现金额" placeholder-class="holder" v-model="form.amount" />
				</view>
				<u-line margin="22rpx 0 32rpx" color="#E2E2E2"></u-line>
				<view class="desc">
					<text>当前余额{{getUserInfo(form.cash_type==1?'distribution_amount':'reward_amount')}}元，</text>
					<text class="all-tixian" @click="onSetAll">全部提现</text>
				</view>
			</view>
		</view>
		<view class="plr35 pb54 pt30 border-box">
			<u-button type="primary" text="确认提现" :customStyle="{ height: '80rpx', borderRadius: '15rpx', fontSize: '30rpx' }"
				@click="onWithdraw" />
		</view>
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue';
	import {
		applyWithdraw, bindingWx
	} from '@/api/user/index';
	import {
		onLoad
	} from "@dcloudio/uni-app";
	import {
		getUserInfo,
		navBack,
		showModel,
		showSuccess,
		showToast
	} from '@/core/app';
	import { useStore } from 'vuex';
	onLoad((options) => {
		if (options.type) {
			form.value.cash_type = options.type
		}
		// #ifndef MP
		judgeWechat()
		// #endif
	})
	
	/* 初始化缓存 */
	const store = useStore();
	function judgeWechat() {
		const userInfo = store.state.user.userInfo;
		if (!userInfo.openid){
			showModel({
				title: "授权绑定微信",
				content: '为了保证您的提现业务安全流畅，本应用需要授权绑定微信，确定前往绑定吗？',
				confirmText: '立即绑定',
				cancelText: '取消提现',
				confirm: () => {
					uni.login({
						provider: 'weixin',
						success: (res) => {
							bindingWx({ code: res.code }).then(res => {
								if (res.code == 200) {
									showSuccess("授权成功！")
								}
							})
						},
						fail: (err) => {
							showToast('微信授权失败')
						},
					})
				}
			})
		}
	}
	
	const form = ref({
		cash_type: 1, // 1=分销佣金(邀请的) 2=奖励金额
		amount: '',
	})
	const onSetAll = () => {
		let userAmount = Number(getUserInfo(form.value.cash_type == 1 ? 'distribution_amount' : 'reward_amount') || 0)
		if (userAmount <= 0) return showToast('当余额不足')
		form.value.amount = userAmount
	}
	const amount = ref(null);
	const onWithdraw = async () => {
		let userAmount = Number(getUserInfo(form.value.cash_type == 1 ? 'distribution_amount' : 'reward_amount') || 0)
		if (!form.value.amount) return showToast('请输入提现金额')
		if (form.value.amount > userAmount) return showToast('可提现余额不足')
		let res = await applyWithdraw(form.value)
		if (res) {
			showSuccess(res.msg)
			navBack()
		}
	}
</script>

<style scoped lang="scss">
	.money {
		padding: 35rpx 30rpx 38rpx;
		box-sizing: border-box;
		background-color: #fff;

		.title {
			font-weight: 400;
			font-size: 30rpx;
			color: #000000;
		}

		.num {
			margin-top: 22rpx;
			display: flex;
			justify-content: flex-start;
			align-items: flex-end;

			.flag {
				font-weight: 400;
				font-size: 37rpx;
				color: #000000;
			}

			.input-ref {
				font-weight: bold;
				font-size: 59rpx;
				color: #000000;
				margin-left: 11rpx;
			}

			.holder {
				color: #eeeeee;
			}
		}

		.desc {
			font-weight: 400;
			font-size: 24rpx;
			color: #8C8C8C;

			.all-tixian {
				color: #0759BB;
			}
		}
	}
</style>